/**
 * 文件接口统一封装
 */
import { message } from 'antd'
import { modalApi } from '@/utils/modalReq'
import { ppostJson } from '@/utils/req'
// 文件上传-测试环境
export const uploadFileUrl = `${modalApi.common}/shopec-systembase/baseattachment/uploadFiles`
/** 获取附件URL */
export function getPublicDownloadUrl(data: any) {
	return ppostJson(
		`${modalApi.common}/shopec-systembase/baseattachment/getLocationDownloadUrl`,
		data,
	)
}

// // 文件上传-滴滴环境
// export const uploadFileUrl = `${modalApi.upload}/shopec-systembase/baseattachment/uploadFromDD`
// export function getPublicDownloadUrl(data: any) {
// 	return ppostJson(
// 		`${modalApi.common}/shopec-systembase/baseattachment/getPublicDownloadUrl`,
// 		data,
// 	)
// }

export const uploadProps = (
	modelName: string,
	targetAttr: number,
	callback: { (key: string): void; (arg0: any): void },
) => ({
	action: uploadFileUrl,
	name: 'files',
	data: {
		modelName,
		targetAttr,
	},
	defaultFileList: [],
	beforeUpload: (file: any) => {
		const isLt20M = file.size / 1024 / 1024 < 200
		if (!isLt20M) {
			message.error('文件必须小于200MB')
		}
		return isLt20M
	},
	onChange(info: any) {
		if (info.file.status === 'uploading') {
			callback('uploading')
		}
		if (info.file.status === 'done') {
			message.success(info?.file?.response.msg)
			callback(info?.fileList?.[0]?.response?.data?.id)
		} else if (info.file.status === 'removed') {
			callback('')
		} else if (info.file.status === 'error') {
			message.error(info?.file?.response.msg)
		}
	},
})

// 多个上传
export const uploadMoreProps = (
	modelName: string,
	targetAttr: number,
	fileList: any,
	callback?: any,
	accept?: any
) => ({
	action: uploadFileUrl,
	name: 'files',
	data: {
		modelName,
		targetAttr,
	},
	fileList,
	defaultFileList: [],
	beforeUpload: (file: any) => {
		const defAccept = ['.pdf', '.docx', '.doc', '.doc', '.xlsx', '.xls', '.zip', '.jpeg', '.jpg', '.png', '.rar']
		const gs = accept || defAccept;
		const lastName = file.name.substring(file.name.lastIndexOf("."))
		if (!gs.includes(lastName)) {
			message.error('文件格式不正确')
		}
		// gs
		const isLt2M = file.size / 1024 / 1024 < 200
		if (!isLt2M) {
			message.error('文件必须小于200MB')
		}
		return isLt2M && lastName
	},
	// eslint-disable-next-line consistent-return
	onChange(info: any) {
		const defAccept = ['.pdf', '.docx', '.doc', '.doc', '.xlsx', '.xls', '.zip', '.jpeg', '.jpg', '.png', '.rar']
		const gs = accept || defAccept;
		const lastName = info.file.name.substring(info.file.name.lastIndexOf("."))
		const isLt200M = info.file.size / 1024 / 1024 < 200
		if (!gs.includes(lastName) || !isLt200M) {
			return false
		}
		if (info?.file?.status === 'uploading') {
			callback('uploading', info?.fileList, info?.file)
		}
		const isLt2M = info.file.size / 1024 / 1024 < 200
		if (isLt2M) {
			callback('setData', info?.fileList, info?.file)
		}
		if (info.file.status === 'done') {
			message.success(`${info.file.name} 上传成功`)
			callback('done', info?.fileList, info?.file)
		} else if (info.file.status === 'error') {
			message.error(`${info.file.name} 上传失败`)
			callback('error', info?.fileList, info?.file)
		}
	},
	showUploadList: {
		showDownloadIcon: true,
	},
	onDownload(info: any) {
		const a = document.createElement('a')
		a.style.display = 'none'
		a.setAttribute('target', '_blank')
		// eslint-disable-next-line no-unused-expressions
		info.response.data.filename &&
			a.setAttribute('download', info.response.data.filename)
		a.href = info?.response?.data?.giftNamespace
			? info?.response?.data?.giftNamespace
			: info.url
		document.body.appendChild(a)
		a.click()
		document.body.removeChild(a)
	},
	onRemove(info: any) {
		callback('remove', info?.fileList, info?.file)
		message.success(`${info?.name}移除成功`, info?.fileList, info?.file)
	},
})
